package com.tdam2011.grupo05.ui.activities.message;

import com.tdam2011.grupo05.R;
import com.tdam2011.grupo05.control.adapters.ThreadCursorAdapter;
import com.tdam2011.grupo05.control.app.TdamApplication;
import com.tdam2011.grupo05.database.content.TdamContentProvider;
import com.tdam2011.grupo05.entities.message.MessageWeb;
import com.tdam2011.grupo05.entities.thread.ThreadWeb;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

public class ThreadListFragment extends ListFragment implements
		LoaderCallbacks<Cursor> {

	public interface ThreadCallback {

		public void onSelectThreadWeb(long idThread);

		void onNewMessage();

		void deleteThread(long idThread);

		void backToDashboard();
	}

	private static final String TAG = "ThreadListFragment";
	private static final String[] PROJECTION = { ThreadWeb.COL_ID,
			ThreadWeb.COL_ID_USER, ThreadWeb.COL_STATE,
			ThreadWeb.COL_TIMESTAMP, ThreadWeb.COL_NAME,
			ThreadWeb.COL_ID_ACCOUNT };

	private ImageView mImageViewBar;

	private ThreadCallback listener;
	private ImageButton mImageButtonNewMenssage;
	private ThreadCursorAdapter mAdapter;

	private View.OnClickListener imageAction = new OnClickListener() {

		@Override
		public void onClick(View v) {
			listener.backToDashboard();
		}
	};

	private OnClickListener newMessageClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (listener != null) {
				listener.onNewMessage();
			}
		}
	};

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		listener = (ThreadCallback) activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_thread_web, container,
				false);
		mImageButtonNewMenssage = (ImageButton) view
				.findViewById(R.id.ib_nuevo_msn);
		mImageButtonNewMenssage.setOnClickListener(newMessageClick);
		mImageViewBar = (ImageView) view.findViewById(R.id.image_thread_web);
		mImageViewBar.setOnClickListener(imageAction);

		return view;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {

		if (listener != null) {
			CursorWrapper cw = (CursorWrapper) getListAdapter().getItem(
					position);
			long idThread = cw.getLong(cw.getColumnIndex(ThreadWeb.COL_ID));
			listener.onSelectThreadWeb(idThread);
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.i(TAG, "onActivityCreated");

		getListView().setOnItemLongClickListener(longClickAction);

		mAdapter = new ThreadCursorAdapter(getActivity(),
				R.layout.thread_web_item_without_message, null,
				new String[] { ThreadWeb.COL_NAME },
				new int[] { android.R.id.text1 }, 0);
		setListAdapter(mAdapter);
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int data, Bundle bundle) {
		return new CursorLoader(getActivity(),
				TdamContentProvider.THREAD_CONTENT_URI, PROJECTION,
				ThreadWeb.COL_ID_ACCOUNT + "=?",
				new String[] { String.valueOf(((TdamApplication) getActivity()
						.getApplication()).getCurrentAccount().getId()) }, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		mAdapter.swapCursor(cursor);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		mAdapter.swapCursor(null);
	}

	protected void showDialogInfo(final int pos) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(getResources()
				.getString(R.string.dialog_delete_option));
		builder.setMessage(R.string.dialog_delete_thread);
		builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				CursorWrapper cw = (CursorWrapper) mAdapter.getItem(pos);
				long id = cw.getLong(cw.getColumnIndex(MessageWeb.COL_ID));
				if (listener != null) {
					listener.deleteThread(id);
					Log.d(TAG, "thread delete=" + id);
				}
				dialog.dismiss();
			}
		});
		builder.setCancelable(true);
		AlertDialog alert = builder.create();
		alert.show();
	}

	private AdapterView.OnItemLongClickListener longClickAction = new AdapterView.OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos,
				long id) {
			showDialogInfo(pos);
			return true;
		}
	};

}
